<!DOCTYPE html>
<html>
<head>
<title>Detached callback of a custom element should not be called if document has no browsing context</title>
<meta name="author" title="Sergey G. Grekhov" href="mailto:sgrekhov@unipro.ru">
<meta name="author" title="Aleksei Yu. Semenov" href="mailto:a.semenov@unipro.ru">
<meta name="assert" content="detached callback ... must be enqueued whenever custom element is removed from the document and this document has a browsing context.">
<link rel="help" href="http://www.w3.org/TR/custom-elements/#types-of-callbacks">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../../testcommon.js"></script>
<link rel="stylesheet" href="/resources/testharness.css">
</head>
<body>
<div id="log"></div>
<script>
test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.registerElement('x-a', {prototype: proto});
    doc.body.innerHTML = '<x-a id="x-a"></x-a>';
    var customElement = doc.querySelector('#x-a');
    doc.body.removeChild(customElement);

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if custom element is created via innerHTML property. ' +
    'Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();
    doc.body.innerHTML = '<x-b id="x-b"></x-b>';
    doc.registerElement('x-b', {prototype: proto});
    var customElement = doc.querySelector('#x-b');
    doc.body.removeChild(customElement);

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if custom element is via innerHTML property before ' +
    'registration of a custom element. Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    doc.body.innerHTML = '<x-c id="x-c"></x-c>';
    var customElement = doc.querySelector('#x-c');

    var proto = newHTMLElementPrototype();
    customElement.constructor.prototype = proto;
    doc.body.removeChild(customElement);

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if custom element is unregistered. ' +
    'Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();

    doc.registerElement('x-d', {prototype: proto});
    doc.body.innerHTML = '<x-d id="x-d"></x-d>';
    doc.body.innerHTML = '';

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if removing custom element via innerHTML property. ' +
    'Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();

    doc.registerElement('x-e', {prototype: proto});
    doc.body.innerHTML = '<div id="customParent"><x-e id="x-e"></x-e></div>';
    var parent = doc.querySelector('#customParent');
    doc.body.removeChild(parent);

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if removing perent of custom element. ' +
    'Document has no browsing context');


test(function() {
    var doc = newHTMLDocument();
    var proto = newHTMLElementPrototype();

    doc.registerElement('x-f', {prototype: proto});
    doc.body.innerHTML = '<div><x-f id="x-f"></x-f></div>';
    doc.body.innerHTML = '';

    assert_equals(proto.detachedCallbackCalledCounter, 0,
        'Callback detached should not be called if the document has no browsing context');
}, 'Test detached callback if removing perent of custom element via innerHTML property. ' +
    'Document has no browsing context');


var loseBrowsingContextTest = async_test('Test detached callback is not called ' +
    'if document lose browsing context and custom element is removed');

loseBrowsingContextTest.step(function() {
    var iframe = newIFrame('../../resources/x-element.html');
    iframe.onload = loseBrowsingContextTest.step_func(function(){
        var doc = iframe.contentDocument;
        var proto = newHTMLElementPrototype();
        doc.registerElement('x-element', {prototype: proto});

        var customElement = doc.querySelector('#x-element');
        iframe.remove();
        customElement.remove();

        assert_equals(proto.detachedCallbackCalledCounter, 0,
            'Callback detached should not be called if the document has no browsing context');
        loseBrowsingContextTest.done();
    });
});


var navigateTest = async_test('Test detached callback is not called, ' +
    'if document\'s window is navigated to another document and custom element is removed');

navigateTest.step(function() {
    var iframe = newIFrame('../../resources/x-element.html');
    iframe.onload = navigateTest.step_func(function() {
        var doc = iframe.contentDocument;
        var proto = newHTMLElementPrototype();
        doc.registerElement('x-element', {prototype: proto});
        customElement = doc.querySelector('#x-element');

        iframe.onload = navigateTest.step_func(function() {
            customElement.remove();
            assert_equals(proto.detachedCallbackCalledCounter, 0,
                'Callback detached should not be called ' +
                'if the document has no browsing context');
            navigateTest.done();
            iframe.remove();
        });
        iframe.src = '../../resources/blank.html';
    });
});
</script>
</body>
</html>
